package com.shengruan.admin.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.shengruan.admin.jdbc.ProductClassJdbc;
import com.shengruan.admin.vo.ProductClass;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;

public class ProductClassAction extends BaseAction {

    private ProductClassJdbc productClassJdbc = (ProductClassJdbc)this.getJdbc(ProductClassJdbc.class);

    /**
     * 添加商品
     * @throws Exception
     */
    public void add() throws Exception {
        String name = this.input("name");

        ProductClass productClass = new ProductClass();
        productClass.setName(StringUtils.trim(name));
        productClass.setCreateTime(System.currentTimeMillis() + "");

        productClassJdbc.save(productClass);
        Constants.productClassList = productClassJdbc.getList();
        this.request.getSession().getServletContext().setAttribute(Constants.PRODUCT_CLASS_LIST,Constants.productClassList);
        this.setRequest(Constants.MESSAGE, "添加商品分类成功。");
        this.redirect("/admin/product/productClass-list.ac");
    }

    public void getById() throws Exception {
        String id = this.input("id");
        ProductClass productClass = productClassJdbc.getById(Integer.valueOf(id));
        this.setRequest("productClass", productClass);

        this.forward("/admin/product/productClass-edit.jsp");
    }

    public void edit() throws Exception {
        String id = this.input("id");
        String name = this.input("name");

         ProductClass productClass = productClassJdbc.getById(Integer.valueOf(id));
         productClass.setName(StringUtils.trim(name));
         productClass.setCreateTime(System.currentTimeMillis() + "");

        productClassJdbc.update(productClass);
        Constants.productClassList = productClassJdbc.getList();
        this.request.getSession().getServletContext().setAttribute(Constants.PRODUCT_CLASS_LIST,Constants.productClassList);
        this.setRequest(Constants.MESSAGE, "修改商品分类信息成功。");
        this.redirect("/admin/product/productClass-list.ac");
    }


    public void list() throws Exception {
        ProductClassJdbc produtClassJdbc = (ProductClassJdbc) this.getJdbc(ProductClassJdbc.class);
        String name = this.input("name");
        List<ProductClass> productClassList = StringUtils.isEmpty(name) ? produtClassJdbc.getList() : produtClassJdbc.getList(name);
        for (ProductClass productClass : productClassList) {
            if (StringUtils.isNotEmpty(productClass.getCreateTime())) {
                    productClass.setCreateTime(DateFormatUtils.format(Long.valueOf(productClass.getCreateTime()), "yyyy-MM-dd HH:mm:ss"));
            }
        }
        setRequest("productClassList", productClassList);
        this.forward("/admin/product/productClass-list.jsp");
    }

    public void delete() throws Exception {
        String id = this.input("id");
        productClassJdbc.deleteById(Integer.valueOf(id));
        Constants.productClassList = productClassJdbc.getList();
        this.request.getSession().getServletContext().setAttribute(Constants.PRODUCT_CLASS_LIST,Constants.productClassList);
        this.setRequest(Constants.MESSAGE, "删除商品分类成功。");
        this.redirect("/admin/product/productClass-list.ac");
    }
}
